package com.aibili.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aibili.pojo.BannerInfo;
import com.aibili.pojo.EventInfo;
import com.aibili.pojo.EventType;
import com.aibili.pojo.EventUser;
import com.aibili.pojo.UserInfo;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;

@Service("eventService")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EventService extends BaseService {
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	/**
	 * 获得banner列表
	 * 
	 * @return
	 */
	public List<BannerInfo> banner() {
		List<BannerInfo> list = (List<BannerInfo>) dataDao.getAllObject(BannerInfo.class);
		return list;
	}

	/**
	 * 添加活动
	 * 
	 * @param event
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void publish(EventInfo event) {
		dataDao.addObject(event);
	}

	/**
	 * 通过id查找user
	 */
	public UserInfo getuser(long id) {
		UserInfo user = (UserInfo) dataDao.getObjectById(UserInfo.class, id);
		return user;

	}

	/**
	 * 通过type得到活动列表
	 * 
	 * @param type
	 */
	@SuppressWarnings("unchecked")
	public List<EventInfo> list(String type, int page) {
		String hql = "from EventInfo where event_type = :event_type order by create_time desc";
		List<EventInfo> list = (List<EventInfo>) dataDao.pageQueryViaParam(hql, 10, page, new String[] { "event_type" },
				type);
		return list;
	}

	/**
	 * 热门活动
	 * 
	 * @return
	 */
	public List<EventType> hot() {
		List<EventType> typelist = dataDao.getAllObject(EventType.class);
		for (EventType eventType : typelist) {
			String sql = " SELECT a.id, a.event_name, a.event_type, a.user_id, a.place, a.intro, a.event_time, a.create_time from t_event_info a right JOIN t_event_user b on a.id = b.event_id and  a.event_type = '"
					+ eventType.getType_name() + "'  GROUP BY a.id order by COUNT(*) DESC";
			List<Object[]> list = (List<Object[]>) dataDao.executeSql(sql);
			List<EventInfo> list2 = new ArrayList<EventInfo>();
			if (list != null && list.size() > 0) {
				if (list.size() <= 3) {
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i)[0] != null) {
							EventInfo eventInfo = new EventInfo();
							eventInfo.setId(CommonUtils.parseLong(list.get(i)[0].toString(), 0));
							eventInfo.setEvent_name(list.get(i)[1].toString());
							eventInfo.setEvent_type(list.get(i)[2].toString());
							UserInfo user = dataDao.getObjectById(UserInfo.class,
									CommonUtils.parseLong(list.get(i)[3].toString(), 0));
							eventInfo.setUser(user);
							eventInfo.setPlace(list.get(i)[4].toString());
							eventInfo.setIntro(list.get(i)[5].toString());
							eventInfo.setEvent_time(
									CommonUtils.getDateFormat(list.get(i)[6].toString(), "yyyy-MM-dd HH:mm:ss"));
							eventInfo.setCreate_time(
									CommonUtils.getDateFormat(list.get(i)[7].toString(), "yyyy-MM-dd HH:mm:ss"));
							list2.add(eventInfo);
						}
					}
				} else {
					for (int i = 0; i < list.size(); i++) {
						if (list2.size() < 3) {
							if (list.get(i)[0] != null) {
								EventInfo eventInfo = new EventInfo();
								eventInfo.setId(CommonUtils.parseLong(list.get(i)[0].toString(), 0));
								eventInfo.setEvent_name(list.get(i)[1].toString());
								eventInfo.setEvent_type(list.get(i)[2].toString());
								UserInfo user = dataDao.getObjectById(UserInfo.class,
										CommonUtils.parseLong(list.get(i)[3].toString(), 0));
								eventInfo.setUser(user);
								eventInfo.setPlace(list.get(i)[4].toString());
								eventInfo.setIntro(list.get(i)[5].toString());
								eventInfo.setEvent_time(
										CommonUtils.getDateFormat(list.get(i)[6].toString(), "yyyy-MM-dd HH:mm:ss"));
								eventInfo.setCreate_time(
										CommonUtils.getDateFormat(list.get(i)[7].toString(), "yyyy-MM-dd HH:mm:ss"));
								list2.add(eventInfo);
							}
						}
					}
				}
			}
			eventType.setEvent_list(list2);
		}
		return typelist;
	}

	/**
	 * 活动详情
	 * 
	 * @param id
	 * @return
	 */
	public EventInfo detail(long id) {
		EventInfo eventinfo = dataDao.getObjectById(EventInfo.class, id);
		return eventinfo;

	}

	/**
	 * 参加活动
	 * 
	 * @param eventuser
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void join(EventUser eventuser) {
		dataDao.addObject(eventuser);
	}

	/**
	 * 参加活动的用户
	 * 
	 * @param eid
	 */
	@SuppressWarnings("unchecked")
	public List<UserInfo> users(long eid) {
		String hql = " from UserInfo where id in (select user.id from EventUser where event_id = :event_id and gift_id = 0)";
		List<UserInfo> userlist = (List<UserInfo>) dataDao.getObjectsViaParam(hql, new String[] { "event_id" }, eid);
		return userlist;
	}

	/**
	 * 发布活动的模板信息
	 * 
	 * @return
	 */
	public List<EventType> info() {
		List<EventType> list = dataDao.getAllObject(EventType.class);
		return list;
	}

	/**
	 * 
	 * @Description 通过 user_id 获取我的活动信息
	 * @param userid
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EventInfo> mine(long userid, int page) {
		String hql = "from EventInfo where user.id = :user_id order by create_time desc";
		List<EventInfo> mylist = (List<EventInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page,
				new String[] { "user_id" }, userid);
		return mylist;
	}

	/**
	 * 
	 * @Description 查找活动类别
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<EventType> getEventType(Integer pageIndex, Integer pageSize) {
		String chql = "select count(u) from EventType u ";
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<EventType> result = new PageBodyModel<EventType>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<EventType> types = new ArrayList<EventType>();
			String hql = " from EventType ";
			types = (List<EventType>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			if (types != null && types.size() > 0) {
				for (EventType eventType : types) {
					if (!CommonUtils.isEmptyString(eventType.getIntro_json())) {
						String intro_json = eventType.getIntro_json();
						String intro = intro_json.substring(1, intro_json.length() - 1);
						intro = intro.replaceAll("\"", "");
						eventType.setIntro_json(intro);
					}
					if (!CommonUtils.isEmptyString(eventType.getTitle_json())) {
						String title_json = eventType.getTitle_json();
						String title = title_json.substring(1, title_json.length() - 1);
						title = title.replaceAll("\"", "");
						eventType.setTitle_json(title);
					}
				}
			}
			result = new PageBodyModel<EventType>(page, types);
		}
		return result;
	}

	/**
	 * 
	 * @Description 通过 id 删除活动类别
	 * @param id
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteEventType(long id) {
		EventType info = dataDao.getObjectById(EventType.class, id);
		if (info != null) {
			dataDao.deleteObjectById(EventType.class, id);
		}
	}

	/**
	 * 
	 * @Description 添加活动类别
	 * @param type_name
	 * @param title_json
	 * @param intro_json
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addEventType(String type_name, String title_json, String intro_json) {
		EventType info = new EventType();
		info.setType_name(type_name);
		info.setTitle_json(title_json);
		info.setIntro_json(intro_json);
		dataDao.addObject(info);
	}

	/**
	 * 
	 * @Description 修改活动类别
	 * @param id
	 * @param type_name
	 * @param title_json
	 * @param intro_json
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateEventType(long id, String type_name, String title_json, String intro_json) {
		EventType info = dataDao.getObjectById(EventType.class, id);
		if (info != null) {
			info.setType_name(type_name);
			info.setTitle_json(title_json);
			info.setIntro_json(intro_json);
			dataDao.updateObject(info);
		}
	}

	/**
	 * 
	 * @Description 条件查询banner信息
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<BannerInfo> getByParams(Integer pageIndex, Integer pageSize) {
		String wherehql = "";
		String chql = " select count(b) from BannerInfo b " + wherehql;
		String hql = " from BannerInfo " + wherehql;

		PageBodyModel<BannerInfo> result = new PageBodyModel<BannerInfo>();
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<BannerInfo> banners = new ArrayList<BannerInfo>();
			banners = (List<BannerInfo>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			if (banners != null && banners.size() > 0) {
				for (BannerInfo bannerInfo : banners) {
					if (!CommonUtils.isEmptyString(bannerInfo.getPic())) {
						bannerInfo.setPic(CommonUtils.getFileRootUrl() + bannerInfo.getPic());
					}
				}
			}
			result = new PageBodyModel<>(page, banners);
		}
		return result;
	}

	/**
	 * 
	 * @Description 通过活动类别（可无）查询活动信息
	 * @param pageIndex
	 * @param pageSize
	 * @param event_type
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<EventInfo> getByType(long user_id, String event_type, Integer pageIndex, Integer pageSize) {
		PageBodyModel<EventInfo> resrlt = new PageBodyModel<EventInfo>();
		List<EventInfo> eventTypes = new ArrayList<EventInfo>();

		String wherehql = " where 1 = 1 ";
		if (user_id != 0) {
			wherehql += " and user_id = " + user_id + " ";
		}
		if (!CommonUtils.isEmptyString(event_type)) {
			wherehql += " and event_type = '" + event_type + "' ";
		}
		String orderhql = " order by create_time desc";
		String chql = " select count(e) from EventInfo e " + wherehql;

		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		if (count > 0) {
			String hql = " from EventInfo " + wherehql + orderhql;
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			eventTypes = (List<EventInfo>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			resrlt = new PageBodyModel<>(page, eventTypes);
		}

		return resrlt;
	}

	/**
	 * 
	 * @Description 通过活动id查询活动用户参加信息
	 * @param event_id
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EventUser> pageByEventid(long event_id, Integer page) {
		String hql = " from EventUser where event_id = :event_id";
		List<EventUser> infos = new ArrayList<EventUser>();
		infos = (List<EventUser>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, new String[] { "event_id" },
				event_id);
		return infos;
	}

	/**
	 * 
	 * @Description 新增 banner
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addBannerInfo(BannerInfo info) {
		dataDao.addObject(info);
	}

	/**
	 * 
	 * @Description 修改 banner
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateBannerInfo(BannerInfo info) {
		dataDao.updateObject(info);
	}

	/**
	 * 
	 * @Description 通过 id 查找 banner
	 * @param id
	 * @return
	 */
	public BannerInfo getBannerById(long id) {
		BannerInfo info = null;
		info = dataDao.getObjectById(BannerInfo.class, id);
		return info;
	}

	/**
	 * 
	 * @Description 通过 id 删除 banner
	 * @param id
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteBannerById(long id) {
		BannerInfo info = null;
		info = dataDao.getObjectById(BannerInfo.class, id);
		if (info != null) {
			dataDao.deleteObject(info);
		}
	}

	/**
	 * 
	 * @Description 通过活动id 获取活动人数
	 * @param event_id
	 * @return
	 */
	public long countEventUserByEventid(long event_id) {
		String hql = "select count(e) from EventUser e where event_id = :event_id and gift_id = 0 ";
		long count = (long) dataDao.getFirstObjectViaParam(hql, new String[] { "event_id" }, event_id);
		return count;
	}

	/**
	 * 
	 * @Description 通过活动id 和用户id 获取参加活动的人
	 * @param event_id
	 * @param user_id
	 * @return
	 */
	public EventUser getEventUserByEventidUserid(long event_id, long user_id) {
		String hql = " from EventUser where event_id = :event_id and user_id = :user_id and gift_id = 0";
		EventUser event = (EventUser) dataDao.getFirstObjectViaParam(hql, new String[] { "event_id", "user_id" },
				event_id, user_id);
		return event;
	}

	/**
	 * 
	 * @Description 通过参加人的id 获取活动信息
	 * @param user_id
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EventInfo> joined(long user_id, int page) {
		String hql = " from EventInfo where id in (select event_id from EventUser where user_id = :user_id and gift_id = 0) order by create_time desc";
		List<EventInfo> infos = (List<EventInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page,
				new String[] { "user_id" }, user_id);
		return infos;
	}

	/**
	 * 
	 * @Description 通过 id 获取活动信息
	 * @param id
	 * @return
	 */
	public EventInfo getEventInfoById(long id) {
		EventInfo info = dataDao.getObjectById(EventInfo.class, id);
		return info;
	}

	/**
	 * 
	 * @Description 通过 id 获取活动类别
	 * @param id
	 * @return
	 */
	public EventType getEventTypeBy(long id) {
		EventType info = dataDao.getObjectById(EventType.class, id);
		return info;
	}

	/**
	 * 
	 * @Description 获取所有的活动类别
	 * @return
	 */
	public List<EventType> getAllEventtype() {
		List<EventType> list = new ArrayList<>();
		list = dataDao.getAllObject(EventType.class);
		return list;
	}

	/**
	 * 
	 * @Description 通过活动id 获取参加人的信息
	 * @param event_id
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<EventUser> getEventUserByEventPage(long event_id, Integer page) {
		PageBodyModel<EventUser> resrlt = new PageBodyModel<EventUser>();
		String wherehql = " where event_id = :event_id and gift_id = 0 ";
		String orderhql = " order by join_time desc ";
		String chql = " select count(e) from EventUser e " + wherehql;
		long count = (long) dataDao.getFirstObjectViaParam(chql, new String[] { "event_id" }, event_id);
		if (count > 0) {
			PageModel model = PageModel.createPage(count, page, DsConstant.PAGESIZE);
			String hql = " from EventUser " + wherehql + orderhql;
			List<EventUser> list = new ArrayList<EventUser>();
			list = (List<EventUser>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, model.getPageIndex(),
					new String[] { "event_id" }, event_id);
			resrlt = new PageBodyModel<>(model, list);
		}
		return resrlt;
	}

	/**
	 * 
	 * @Description 获取所有的 banner
	 * @return
	 */
	public List<BannerInfo> getAllBanner() {
		List<BannerInfo> infos = new ArrayList<>();
		infos = dataDao.getAllObject(BannerInfo.class);
		if (infos != null && infos.size() > 0) {
			for (BannerInfo bannerInfo : infos) {
				if (!CommonUtils.isEmptyString(bannerInfo.getPic())) {
					bannerInfo.setPic(CommonUtils.getFileRootUrl() + bannerInfo.getPic());
				}
			}
		}
		return infos;
	}

	/**
	 * 
	 * @Description 新增 用户参加活动信息
	 * @param eventuser
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addEventUser(EventUser eventuser) {
		dataDao.addObject(eventuser);
	}

}
